'''
@author: harumonia
@site: 
@datetime: 2020/4/19 9:39
@software: PyCharm
'''

import pytest

from application import creat_app, db


@pytest.fixture
def app():
    """Create and configure a new app instance for each test."""
    app = creat_app('test')

    # create the database and load test data
    with app.app_context():
        db.drop_all()
        db.create_all()

    yield app


@pytest.fixture
def client(app):
    """A test client for the app."""
    return app.test_client()


# @pytest.fixture
# def runner(app):
#     """A test runner for the app's Click commands."""
#     return app.test_cli_runner()


# class AuthActions:
#     def __init__(self, client):
#         self._client = client
#
#     def login(self, email="zxjjjj", password="123456"):
#         return self._client.post(
#             "/login", data={"email": email, "password": password}
#         )
#
#     def logout(self):
#         return self._client.get("/logout")
#
#
# @pytest.fixture
# def auth(client):
#     return AuthActions(client)


# def get_db():
#     """Connect to the application's configured database. The connection
#     is unique for each request and will be reused if this is called
#     again.
#     """
#     if "db" not in g:
#         g.db = sqlite3.connect(
#             current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES
#         )
#         g.db.row_factory = sqlite3.Row
#
#     return g.db
#
#
# def close_db(e=None):
#     """If this request connected to the database, close the
#     connection.
#     """
#     db = g.pop("db", None)
#
#     if db is not None:
#         db.close()
#
#
# def init_db():
#     """Clear existing data and create new tables."""
#     db = get_db()
#
#     with current_app.open_resource("schema.sql") as f:
#         db.executescript(f.read().decode("utf8"))
#
#
# @click.command("init-db")
# @with_appcontext
# def init_db_command():
#     """Clear existing data and create new tables."""
#     init_db()
#     click.echo("Initialized the database.")
